package com.example.authsystem.repository;

import com.example.authsystem.entity.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Tag(name = "用户数据访问接口", description = "用于管理用户实体的持久化操作")
@Repository
public interface UserRepository extends JpaRepository<User, Long> {

    /**
     * 根据用户名查找用户（忽略大小写）
     */
    @Operation(
            summary = "根据用户名查询用户",
            description = "通过用户名精确匹配查找用户信息，不区分大小写",
            responses = @ApiResponse(
                    responseCode = "200",
                    description = "返回匹配的用户对象",
                    content = @Content(schema = @Schema(implementation = User.class))
            )
    )
    Optional<User> findByUsernameIgnoreCase(@Parameter(description = "用户名", example = "john_smith") String username);

    /**
     * 根据邮箱查找用户（忽略大小写）
     */
    @Operation(
            summary = "根据邮箱查询用户",
            description = "通过邮箱地址精确匹配查找用户，用于登录或找回密码场景",
            responses = @ApiResponse(
                    responseCode = "200",
                    description = "返回匹配的用户对象",
                    content = @Content(schema = @Schema(implementation = User.class))
            )
    )
    Optional<User> findByEmailIgnoreCase(String email);

    /**
     * 根据手机号查找用户（忽略大小写）
     */
    @Operation(
            summary = "根据手机号查询用户",
            description = "通过手机号精确匹配查找用户，用于登录或找回密码场景",
            responses = @ApiResponse(
                    responseCode = "200",
                    description = "返回匹配的用户对象",
                    content = @Content(schema = @Schema(implementation = User.class))
            )
    )
    Optional<User> findByPhone(String phone);

    /**
     * 统计指定月份内创建的用户数量
     */
    @Operation(
            summary = "统计某月新增用户数",
            description = "计算在给定月份内注册并创建账户的用户总数",
            responses = @ApiResponse(
                    responseCode = "200",
                    description = "返回用户数量",
                    content = @Content(schema = @Schema(type = "integer", example = "42"))
            )
    )
    @Query("SELECT COUNT(u) FROM User u WHERE u.createTime >= :start AND u.createTime < :end")
    long countUsersInMonth(
            @Param("start") @Parameter(description = "起始时间（包含）", example = "2024-01-01T00:00:00") LocalDateTime start,
            @Param("end") @Parameter(description = "结束时间（不包含）", example = "2024-02-01T00:00:00") LocalDateTime end
    );

    /**
     * 查询最近注册的N个用户
     */
    @Operation(
            summary = "获取最新注册用户列表",
            description = "按创建时间倒序返回最近注册的用户"
    )
    List<User> findTop10ByOrderByCreateTimeDesc();
}
